package com.example.howard.xiao7music.ui;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.howard.xiao7music.MyApplication;
import com.example.howard.xiao7music.R;
import com.example.howard.xiao7music.bean.SingerSongBean;
import com.example.howard.xiao7music.bean.SongDownloadBean;
import com.example.howard.xiao7music.bean.SongRankingBean;
import com.example.howard.xiao7music.model.GetRemoteSource;
import com.example.howard.xiao7music.service.MusicPlayerService;
import com.example.howard.xiao7music.ui.Activity.PlayActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Howard on 2016/9/25.
 */
public class BottomPlayer extends Fragment {
    private GetRemoteSource getRemoteSource=new GetRemoteSource(new GetRemoteSource.callback() {

        @Override
        public void setRankingSongBean(SongRankingBean bean) {

        }

        @Override
        public void setSingerSongBean(SingerSongBean bean) {

        }

        @Override
        public void setPlaySongURL(String songurl) {

            url=songurl;
            asURLplay(url);
        }

        @Override
        public void setDownloadSongBean(SongDownloadBean bean) {

        }
    });
    private ImageButton songImage;
    private LinearLayout songInfo;
    private TextView songName;
    private TextView singerName;
    private ImageButton pre;
    private ImageButton playAndpause;
    private ImageButton next;
    private MusicPlayerService musicPlayerService;
    private MyApplication myApplication;
    //用于服务播放的歌曲列表
    private List<Map<String, Object>> songList=new ArrayList<>();
    //当前播放的歌曲map
    private Map<String, Object> map=new HashMap<>();
    //当前的list位置
    private int position;
    private String url;
    private String song;
    private String singer;


    public BottomPlayer() {
        super();
        if (myApplication!=null &musicPlayerService!=null){
            if (myApplication.songdatalist!=null){
                songList=myApplication.getSongdatalist();
            }
        }

    }

//    public BottomPlayer newInstance(BottomPlayer player){
//
//        BottomPlayer bottomPlayer=new BottomPlayer();
//        bottomPlayer.myApplication=player.myApplication;
//        bottomPlayer.songList=player.songList;
//        bottomPlayer.musicPlayerService=player.musicPlayerService;
//        bottomPlayer.songName=player.songName;
//        bottomPlayer.singerName=player.singerName;
//        bottomPlayer.map=player.map;
//        bottomPlayer.position=player.position;
//        bottomPlayer.getState(bottomPlayer.getView());
//
//
//
//        return bottomPlayer;
//    }

    @Nullable
    @Override
    public View getView() {
        return super.getView();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.bottomplayer, null);
        setView(view);



        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        myApplication= (MyApplication) getActivity().getApplication();
        musicPlayerService=myApplication.getMusicPlayerService();
        if (map.get("SONG")!=null){
            String song=map.get("SONG").toString();
            String singer=map.get("SINGER").toString();

            if (musicPlayerService!=null){
                songName.setText(song);
                singerName.setText(singer);
                if (musicPlayerService.callIsPlaying()){
                    playAndpause.setBackground(this.getResources().getDrawable(R.drawable.mini_pause));
                }else {
                    playAndpause.setBackground(this.getResources().getDrawable(R.drawable.mini_play_btn_view));
                }
            }
        }
    }

    private void setView (View view){
        songImage= (ImageButton) view.findViewById(R.id.main_mini_image);
        songInfo= (LinearLayout) view.findViewById(R.id.main_song);
        songName= (TextView) view.findViewById(R.id.main_song_name);
        singerName= (TextView) view.findViewById(R.id.main_singer_name);
        pre= (ImageButton) view.findViewById(R.id.main_pre);
        playAndpause= (ImageButton) view.findViewById(R.id.main_play_or_pause);
        next= (ImageButton) view.findViewById(R.id.main_next);

        songImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent=new Intent(getActivity(),PlayActivity.class);
                startActivity(intent);
            }
        });

        songInfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent=new Intent(getActivity(),PlayActivity.class);
                startActivity(intent);
            }
        });

        pre.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (songList!=null){
                    preClick();
                }

            }
        });

        playAndpause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (songList!=null){
                    playandpauseClick();
                }

            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (songList!=null){
                    nextClick();
                }

            }
        });
    }
    //响应播放暂定按钮
    private void playandpauseClick(){
        if (musicPlayerService!=null){

            if (musicPlayerService.callIsPlaying()){
                musicPlayerService.callPause();
                playAndpause.setBackground(this.getResources().getDrawable(R.drawable.mini_play_btn_view));
            }else if (!musicPlayerService.callMediaIsNull()){
                musicPlayerService.callrestart();
                playAndpause.setBackground(this.getResources().getDrawable(R.drawable.mini_pause));
            }
        }

    }

    private void preClick(){
        if (musicPlayerService!=null){

            if (!musicPlayerService.callMediaIsNull()){
                if(position==0){
                    position=songList.size()-1;
                }else {
                    position--;
                }
                map=songList.get(position);
                playSong(map);
            }

        }
    }

    private void nextClick(){
        if (musicPlayerService!=null){
            if (!musicPlayerService.callMediaIsNull()){
                if (position==songList.size()-1){
                    position=0;
                }else {
                    position++;
                }
                map=songList.get(position);
                playSong(map);
            }
        }
    }
    //播放歌曲并传递歌曲列表给服务
    private void playSong (Map<String, Object> map){
        boolean isLocal;
        isLocal= getURL(map);
        song=map.get("SONG").toString();
        singer=map.get("SINGER").toString();

        if (musicPlayerService!=null&&isLocal){
            musicPlayerService.callplay(url);
            songName.setText(song);
            singerName.setText(singer);
            playAndpause.setBackground(this.getResources().getDrawable(R.drawable.mini_pause));
        }
    }

    //用于异步播放歌曲
    private void asURLplay (String url){
        if (musicPlayerService!=null){
            musicPlayerService.callplay(url);
            songName.setText(song);
            singerName.setText(singer);
            playAndpause.setBackground(this.getResources().getDrawable(R.drawable.mini_pause));
        }
    }

    //用于newInstance
//    private void getState (View view){
//        String song;
//        String singer;
//        if (map.get("SONG")!=null){
//            song=map.get("SONG").toString();
//            singer=map.get("SINGER").toString();
//        }else {
//            song="未命名";
//            singer="未命名";
//        }
//
//        songName= (TextView) view.findViewById(R.id.main_song_name);
//        singerName= (TextView) view.findViewById(R.id.main_singer_name);
//        songName.setText(song);
//        singerName.setText(singer);
//        playandpauseClick();
//    }




    public void callByList(int pos){
        songList=myApplication.getSongdatalist();
        if (map.equals(songList.get(pos))){

            if (musicPlayerService.callIsPlaying()){
                musicPlayerService.callPause();
                playAndpause.setBackground(this.getResources().getDrawable(R.drawable.mini_play_btn_view));
            }else {
                musicPlayerService.callrestart();
                playAndpause.setBackground(this.getResources().getDrawable(R.drawable.mini_pause));
            }
        }else {
            position=pos;
            map=songList.get(pos);
            playSong(map);
        }




    }


    private boolean getURL (Map<String, Object> map){
//        String url;
        if (map.get("URL")!=null){
            url=map.get("URL").toString();
            return true;
        }else {
            getRemoteSource.getPlay(map.get("ID").toString());
            return false;
        }
//        return url;
    }

    public MusicPlayerService getMusicPlayerService() {
        return musicPlayerService;
    }

    public void setMusicPlayerService(MusicPlayerService musicPlayerService) {
        this.musicPlayerService = musicPlayerService;
    }

    public List<Map<String, Object>> getSongList() {
        return songList;
    }

    public void setSongList(List<Map<String, Object>> songList) {
        this.songList = songList;
    }

    public Map<String, Object> getMap() {
        return map;
    }

    public void setMap(Map<String, Object> map) {
        this.map = map;
    }

    public int getPosition() {
        return position;
    }

    public void setPosition(int position) {
        this.position = position;
    }

    public void setSongAndSinger (){
        if (map.get("SONG")!=null){
            String song=map.get("SONG").toString();
            String singer=map.get("SINGER").toString();

            if (musicPlayerService!=null){
                songName.setText(song);
                singerName.setText(singer);
            }
        }
    }
}
